<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺地址" prop="shopAddress">
        <el-input
          v-model="queryParams.shopAddress"
          placeholder="请输入店铺地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型" prop="shopBooksStartic">
        <el-select v-model="queryParams.shopBooksStartic" placeholder="请选择商品类型" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in CategoryList " :key="index" :label="item.categoryName"
                     :value="item.categoryId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="店铺开始营业时间" prop="shopBeginHours">
        <el-input
          v-model="queryParams.shopBeginHours"
          placeholder="请输入店铺开始营业时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺关门时间" prop="shopEndHours">
        <el-input
          v-model="queryParams.shopEndHours"
          placeholder="请输入店铺关门时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺是否营业" prop="shopStoreStatic" >
        <el-input
          v-model="shopStatusDisplay"
          placeholder="请输入店铺是否营业"
          clearable
          @input="handleStoreStaticInput"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺联系电话" prop="shopPhone">
        <el-input
          v-model="queryParams.shopPhone"
          placeholder="请输入店铺联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:shop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:shop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:shop:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:shop:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺标识" align="center" prop="shopId" />
      <el-table-column label="店铺地址" align="center" prop="shopAddress" />
      <el-table-column label="店铺名称" align="center" prop="shopName" />
      <el-table-column label="店铺所买商品类型" align="center" prop="shopBooksStartic" />
      <el-table-column label="店铺logo" align="center" prop="shopLogo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.shopLogo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺营业执照" align="center" prop="shopLicensePrice" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.shopLicensePrice" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺开始营业时间" align="center" prop="shopBeginHours" width="180" />
      <el-table-column label="店铺关门时间" align="center" prop="shopEndHours" width="180" />
        <el-table-column label="店铺是否营业" align="center" prop="shopStoreStatic">
          <template slot-scope="scope">
            {{ scope.row.shopStoreStatic === 1 ? '营业中' : '已关门' }}
          </template>
        </el-table-column>
      <el-table-column label="店铺联系电话" align="center" prop="shopPhone" />
      <el-table-column label="店铺商品数量" align="center" prop="shopGoodsNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:shop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:shop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺入驻对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺地址" prop="shopAddress">
          <el-input v-model="form.shopAddress" placeholder="请输入店铺地址" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="商品类型" prop="shopBooksStartic">
          <el-select v-model="form.shopBooksStartic" placeholder="请选择商品类型" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in CategoryList " :key="index" :label="item.categoryName"
                       :value="item.categoryId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺logo" prop="shopLogo">
          <image-upload v-model="form.shopLogo"/>
        </el-form-item>
        <el-form-item label="店铺营业执照" prop="shopLicensePrice">
          <image-upload v-model="form.shopLicensePrice"/>
        </el-form-item>
        <el-form-item label="店铺开始营业时间" prop="shopBeginHours">
          <el-input
            v-model="form.shopBeginHours"
            placeholder="请输入店铺开始营业时间"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="店铺关门时间" prop="shopEndHours">
          <el-input
            v-model="form.shopEndHours"
            placeholder="请输入店铺关门时间"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="店铺联系电话" prop="shopPhone">
          <el-input v-model="form.shopPhone" placeholder="请输入店铺联系电话" />
        </el-form-item>
        <el-form-item label="店铺商品数量" prop="shopGoodsNumber">
          <el-input v-model="form.shopGoodsNumber" placeholder="请输入店铺商品数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShop, getShop, delShop, addShop, updateShop,listCategory } from "@/api/shop/shop";

export default {
  name: "Shop",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺入驻表格数据
      shopList: [],
      // 分类列表数据
      CategoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      shopStatusDisplay : null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopAddress: null,
        shopName: null,
        shopBooksStartic: null,
        shopLicensePrice: null,
        shopBeginHours: null,
        shopEndHours: null,
        shopStoreStatic: null,
        shopPhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleStoreStaticInput(value){
      this.shopStatusDisplay = value;
      if (value.includes('营业中')) {
        this.queryParams.shopStoreStatic = 1;
      }else if (value.includes('营')) {
        this.queryParams.shopStoreStatic = 1;
      }else if (value.includes('业')) {
        this.queryParams.shopStoreStatic = 1;
      }else if (value.includes('中')) {
        this.queryParams.shopStoreStatic = 1;
      }else if (value.includes('营业')) {
        this.queryParams.shopStoreStatic = 1;
      }else if (value.includes('营中')) {
        this.queryParams.shopStoreStatic = 1;
      }else if (value.includes('业中')) {
        this.queryParams.shopStoreStatic = 1;
      }else if (value.includes('已关门')) {
        this.queryParams.shopStoreStatic = 2;
      }else if (value.includes('已')) {
        this.queryParams.shopStoreStatic = 2;
      }else if (value.includes('关')) {
        this.queryParams.shopStoreStatic = 2;
      }else if (value.includes('门')) {
        this.queryParams.shopStoreStatic = 2;
      }else if (value.includes('关门')) {
        this.queryParams.shopStoreStatic = 2;
      }else if (value.includes('已关')) {
        this.queryParams.shopStoreStatic = 2;
      }else if (value.includes('门关')) {
        this.queryParams.shopStoreStatic = 2;
      } else {
        this.queryParams.shopStoreStatic = null; // 如果输入不符合预期，重置为 null
      }
    },
    /** 查询店铺入驻列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listCategory().then(response=>{
        this.CategoryList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shopId: null,
        shopAddress: null,
        shopName: null,
        shopBooksStartic: null,
        shopLogo: null,
        shopLicensePrice: null,
        shopBeginHours: null,
        shopEndHours: null,
        shopStoreStatic: null,
        shopPhone: null,
        shopGoodsId: null,
        shopGoodsNumber: null,
        shopBusinessId: null,
        shopExtend3: null,
        shopExtend4: null,
        shopExtend5: null,
        shopExtend6: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.shopId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺入驻";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shopId = row.shopId || this.ids
      getShop(shopId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺入驻";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.shopId != null) {
            updateShop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shopIds = row.shopId || this.ids;
      this.$modal.confirm('是否确认删除店铺入驻编号为"' + shopIds + '"的数据项？').then(function() {
        return delShop(shopIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/shop/export', {
        ...this.queryParams
      }, `shop_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
